﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.IO.IsolatedStorage;
using System.Windows.Media;
using Moneyman.Enums;
using Moneyman.Infrastructure;
using System.Windows.Media.Imaging;
using System.Windows;
using System.IO;

namespace Moneyman.DataModel
{
    public class SettingsKeys
    {
        public const string Language = "Language";
        public const string Location = "Location";
        public const string AutoSavePlace = "AutoSavePlace";
        public const string ImageName = "ImageName";
        public const string NitifyDays = "NotifyDays";
        public const string ImageStretch = "ImageStretch";
    }

    public class SettingsInfo : NotificationObject
    {
        public SettingsInfo()
        {
            if (!DesignerProperties.IsInDesignTool)
            {
                CheckSettings();
            }
        }

        #region Fields
        private string backgroundImage = "/Moneyman.Wallet;component/Assets/background.png";
        private string language = "zh-CN";
        private ImageLocationValue location = ImageLocationValue.Applocation;
        private bool autoSavePlace = true;
        private Stretch imageStretch = Stretch.UniformToFill;
        private int notifyDays = 7;
        #endregion

        #region Properties
        public string BackgroundImage
        {
            get { return backgroundImage; }
            set
            {
                backgroundImage = value;
                IsolatedStorageSettings.ApplicationSettings[SettingsKeys.ImageName] = value;
                IsolatedStorageSettings.ApplicationSettings.Save();
                RaisePropertyChanged(() => BackgroundImage);
                RaisePropertyChanged(() => BackgroundImageSource);
            }
        }

        public string Language
        {
            get { return language; }
            set
            {
                language = value;
                IsolatedStorageSettings.ApplicationSettings[SettingsKeys.Language] = value;
                IsolatedStorageSettings.ApplicationSettings.Save();
                RaisePropertyChanged(() => Language);
            }
        }

        public ImageLocationValue Location
        {
            get { return location; }
            set
            {
                location = value;
                IsolatedStorageSettings.ApplicationSettings[SettingsKeys.Location] = value;
                IsolatedStorageSettings.ApplicationSettings.Save();
                RaisePropertyChanged(() => Location);
            }
        }

        public bool AutoSavePlace
        {
            get
            {
                return autoSavePlace;
            }
            set
            {
                autoSavePlace = value;
                IsolatedStorageSettings.ApplicationSettings[SettingsKeys.AutoSavePlace] = value;
                IsolatedStorageSettings.ApplicationSettings.Save();
                RaisePropertyChanged(() => AutoSavePlace);
            }
        }

        public Stretch ImageStretch
        {
            get { return imageStretch; }
            set
            {
                imageStretch = value;
                IsolatedStorageSettings.ApplicationSettings[SettingsKeys.ImageStretch] = value;
                IsolatedStorageSettings.ApplicationSettings.Save();
                RaisePropertyChanged(() => ImageStretch);
            }
        }

        public int NotifyDays
        {
            get { return notifyDays; }
            set
            {
                notifyDays = value;
                IsolatedStorageSettings.ApplicationSettings[SettingsKeys.NitifyDays] = value;
                IsolatedStorageSettings.ApplicationSettings.Save();
                RaisePropertyChanged(() => NotifyDays);
            }
        }

        public IEnumerable<Stretch> StretchCollection
        {
            get
            {
                yield return Stretch.Fill;
                yield return Stretch.None;
                yield return Stretch.Uniform;
                yield return Stretch.UniformToFill;
            }
        }

        public IEnumerable<string> ExistsLanguageFile
        {
            get
            {
                if (DesignerProperties.IsInDesignTool)
                {
                    yield return "zh-CN";
                }
                else
                {
                    using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        foreach (string file in store.GetFileNames(@"\Languages\*.xaml"))
                        {
                            yield return Path.GetFileNameWithoutExtension(file);
                        }
                    }
                }
            }
        }

        public ImageSource BackgroundImageSource
        {
            get
            {
                BitmapImage image = new BitmapImage();
                if (Location != ImageLocationValue.Isolated)
                {
                    image.SetSource(Application.GetResourceStream(new Uri(BackgroundImage, UriKind.RelativeOrAbsolute)).Stream);
                    //try
                    //{
                    //    image.SetSource(Application.GetResourceStream(new Uri(BackgroundImage, UriKind.RelativeOrAbsolute)).Stream);
                    //}
                    //catch
                    //{
                    //    BackgroundImage = "/Omnia.Wallet;component/Assets/desert.png";
                    //    Location = ImageLocationValue.Applocation;
                    //    image.SetSource(Application.GetResourceStream(new Uri(BackgroundImage, UriKind.RelativeOrAbsolute)).Stream);
                    //}
                }
                else
                {
                    using (Stream stream = IsolatedStorageFile.GetUserStoreForApplication().OpenFile(BackgroundImage, FileMode.Open, FileAccess.Read))
                    {
                        image.SetSource(stream);
                    }
                }
                return image;
            }
        }

        #endregion

        private void CheckSettings()
        {
            IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

            #region Background
            if (!settings.Contains(SettingsKeys.ImageName))
            {
                settings.Add(SettingsKeys.ImageName, backgroundImage);
                settings.Save();
            }
            else
            {
                BackgroundImage = settings[SettingsKeys.ImageName].ToString();
            }
            #endregion

            #region Language
            if (!settings.Contains(SettingsKeys.Language))
            {
                settings.Add(SettingsKeys.Language, language);
                settings.Save();
            }
            else
            {
                Language = settings[SettingsKeys.Language].ToString();
            }
            #endregion

            #region Image Location
            if (!settings.Contains(SettingsKeys.Location))
            {
                settings.Add(SettingsKeys.Location, location);
                settings.Save();
            }
            else
            {
                Location = (ImageLocationValue)settings[SettingsKeys.Location];
            }
            #endregion

            #region AutoSavePlace
            if (!settings.Contains(SettingsKeys.AutoSavePlace))
            {
                settings.Add(SettingsKeys.AutoSavePlace, autoSavePlace);
                settings.Save();
            }
            else
            {
                AutoSavePlace = (bool)settings[SettingsKeys.AutoSavePlace];
            }
            #endregion

            #region BackgroundImageStretch
            if (!settings.Contains(SettingsKeys.ImageStretch))
            {
                settings.Add(SettingsKeys.ImageStretch, imageStretch);
                settings.Save();
            }
            else
            {
                ImageStretch = (Stretch)settings[SettingsKeys.ImageStretch];
            }
            #endregion

            #region NotifyDays
            if (!settings.Contains(SettingsKeys.NitifyDays))
            {
                settings.Add(SettingsKeys.NitifyDays, notifyDays);
                settings.Save();
            }
            else
            {
                NotifyDays = int.Parse(settings[SettingsKeys.NitifyDays].ToString());
            }
            #endregion
        }

        #region Event
        public event EventHandler SettingsChanged = delegate { };

        protected override void RaisePropertyChanged(string propertyName)
        {
            base.RaisePropertyChanged(propertyName);
            SettingsChanged.Invoke(this, new EventArgs());
        }

        public void Reset()
        {
            IsolatedStorageSettings.ApplicationSettings.Clear();
            IsolatedStorageSettings.ApplicationSettings.Add(SettingsKeys.AutoSavePlace, true);
            IsolatedStorageSettings.ApplicationSettings.Add(SettingsKeys.ImageName, "/Moneyman.Wallet;component/Assets/background.png");
            IsolatedStorageSettings.ApplicationSettings.Add(SettingsKeys.ImageStretch, Stretch.UniformToFill);
            IsolatedStorageSettings.ApplicationSettings.Add(SettingsKeys.Language, "zh-CN");
            IsolatedStorageSettings.ApplicationSettings.Add(SettingsKeys.Location, ImageLocationValue.Applocation);
            IsolatedStorageSettings.ApplicationSettings.Add(SettingsKeys.NitifyDays, 7);
            IsolatedStorageSettings.ApplicationSettings.Save();

            CheckSettings();
        }
        #endregion
    }
}
